package cn.lll.vuehouse.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.lll.vuehouse.FORM.RolePermissionForm;
import cn.lll.vuehouse.common.ResponseResult;
import cn.lll.vuehouse.common.ResponseStatus;
import cn.lll.vuehouse.dto.RolePageDTO;
import cn.lll.vuehouse.entity.Role;
import cn.lll.vuehouse.entity.RolePermission;
import cn.lll.vuehouse.entity.UserRole;
import cn.lll.vuehouse.mapper.RoleMapper;
import cn.lll.vuehouse.mapper.RolePermissionMapper;
import cn.lll.vuehouse.mapper.UserRoleMapper;
import cn.lll.vuehouse.service.RoleService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;

/**
* @author asus-pc
* @description 针对表【sys_role】的数据库操作Service实现
* @createDate 2023-09-01 11:50:31
*/
@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role>
    implements RoleService{


    @Autowired
    private RoleMapper roleMapper;

    @Autowired
    private UserRoleMapper userRoleMapper;

    @Autowired
    private RolePermissionMapper rolePermissionMapper;

    @Override
    public ResponseResult getRoleList(RolePageDTO rolePageDTO) {

        ResponseResult responseResult = new ResponseResult();

        Page<Role> rolePage = new Page<>(rolePageDTO.getCurrentPage(),rolePageDTO.getLimit());

        LambdaQueryWrapper<Role> lambda = new QueryWrapper<Role>().lambda();
        lambda.like(StringUtils.isNotBlank(rolePageDTO.getName()),Role::getName,rolePageDTO.getName());

        roleMapper.selectPage(rolePage,lambda);

        long total = rolePage.getTotal();
        List<Role> records = rolePage.getRecords();

        HashMap<String, Object> map = new HashMap<>();
        map.put("total",total);
        map.put("pageData",records);
        responseResult.success(ResponseStatus.SUCCESS,map);
        return responseResult;
    }

    @Override
    public ResponseResult updateRoleState(Role role) {
        ResponseResult responseResult = new ResponseResult();
        roleMapper.updateById(role);
        responseResult.success(ResponseStatus.SUCCESS);
        return responseResult;
    }

    @Override
    public ResponseResult addRole(Role role) {

        ResponseResult responseResult = new ResponseResult();
        // 判断角色名是否重复
        LambdaQueryWrapper<Role> lambda = new QueryWrapper<Role>().lambda();
        lambda.eq(Role::getName,role.getName())
                .or()
                .eq(Role::getRoleEn,role.getRoleEn());
        List<Role> roles = roleMapper.selectList(lambda);
        if(ObjectUtil.isNotEmpty(roles)){

            // 存在
            responseResult.fail(ResponseStatus.ROLE_FAIL);
            return responseResult;
        }
        roleMapper.insert(role);
        responseResult.success(ResponseStatus.ADD_SUCCESS);
        return responseResult;
    }

    @Override
    public ResponseResult updateRole(Role role) {
        ResponseResult responseResult = new ResponseResult();

        LambdaQueryWrapper<Role> lambda = new QueryWrapper<Role>().lambda();
        lambda.eq(Role::getName,role.getName())
                .eq(Role::getRoleEn,role.getRoleEn());
        List<Role> roles = roleMapper.selectList(lambda);
        if(ObjectUtil.isNotEmpty(roles)){

            // 存在
            responseResult.fail(ResponseStatus.ROLE_FAIL);
            return responseResult;
        }
        roleMapper.updateById(role);
        responseResult.success(ResponseStatus.UPDATE_SUCCESS);
        return responseResult;
    }

    @Override
    public ResponseResult getRoleById(Integer id) {

        ResponseResult responseResult = new ResponseResult();
        Role role = roleMapper.selectById(id);
        responseResult.success(ResponseStatus.SUCCESS,role);
        return responseResult;
    }


    @Override
    public ResponseResult deleteRoleById(Integer id) {
        ResponseResult responseResult = new ResponseResult();

        // 根据id查询用户角色关联表
        LambdaQueryWrapper<UserRole> lambda = new QueryWrapper<UserRole>().lambda();
        lambda.eq(UserRole::getRid,id);
        List<UserRole> userRoles = userRoleMapper.selectList(lambda);
        // 判断是否为空
        if(ObjectUtil.isNotEmpty(userRoles)){
            // 不为空 不能删除
            responseResult.fail(ResponseStatus.ROLE_EXIST);
            return responseResult;
        }
        // 根据id删除角色表信息
        roleMapper.deleteById(id);
        // 删除用户角色关联表信息
        userRoleMapper.delete(lambda);
        // 删除角色权限关联表信息
        LambdaQueryWrapper<RolePermission> lambda1 = new QueryWrapper<RolePermission>().lambda();
        lambda1.eq(RolePermission::getRid,id);
        rolePermissionMapper.delete(lambda1);
        responseResult.success(ResponseStatus.DELETE_SUCCESS);
        return responseResult;
    }

    @Override
    public ResponseResult addRolePermission(RolePermissionForm rolePermissionForm) {
        ResponseResult responseResult = new ResponseResult();
        // 根据roleId查询角色权限关联表
        LambdaQueryWrapper<RolePermission> lambda = new QueryWrapper<RolePermission>().lambda();
        lambda.eq(RolePermission::getRid,rolePermissionForm.getRoleId());
        // 删除角色权限关联表信息
        rolePermissionMapper.delete(lambda);


        // 添加新权限
        List<Integer> ids = rolePermissionForm.getIds();
        ids.forEach(pid->{
            RolePermission rolePermission = new RolePermission();
            rolePermission.setRid(rolePermissionForm.getRoleId());
            rolePermission.setPid(pid);
            rolePermissionMapper.insert(rolePermission);
        });

        responseResult.success(ResponseStatus.ADD_SUCCESS);

        return responseResult;
    }

    @Override
    public ResponseResult getRolesList() {
        ResponseResult responseResult = new ResponseResult();
        List<Role> lists = roleMapper.selectList(null);
        responseResult.success(ResponseStatus.SUCCESS, lists);
        return responseResult;
    }
}




